#include <iostream>
using namespace std;

//懒汉式
class Sigleton {
public:
	//3) 静态成员函数访问单例对象
	static Sigleton& getIntance (void) {
		if (s_instance == NULL) {
			s_instance = new Sigleton(1234);
		}
		++s_counter;
		return *s_instance;
	}
	//最后一个访问单例对象的负责释放单例对象
	void release (void) {
		if (--s_counter == 0) {
			delete s_instance;
			s_instance = NULL;
		}
	}
private:
	int m_data;
	//1) 私有化构造函数
	Sigleton (int data = 0):m_data(data) {
		cout << "单例对象被创建了" << endl;
	}
	~Sigleton (void) {
		cout << "单例对象被销毁了" << endl;
	}
	//2) 静态成员变量维护唯一的对象
	static Sigleton* s_instance;
	//记录使用单例对象的个数
	static int s_counter;
};

Sigleton* Sigleton::s_instance = NULL;
int Sigleton::s_counter = 0;

int main (void)
{
	Sigleton& s1 = Sigleton::getIntance();
	Sigleton& s2 = Sigleton::getIntance();
	Sigleton& s3 = Sigleton::getIntance();
	s1.release ();
	s2.release ();
	s3.release ();
	cout << &s1 << endl;
	cout << &s2 << endl;
	cout << &s3 << endl;

	return 0;
}
